﻿package Interface
{
	import Core.Page;
	import Core.PlayerInfo;
	
	import Interface.TracksClasses.TrackButton;
	import Interface.StatisticsClassses.StatisticsAvatar;
	
	import Gameplay.PointsStatisticInfo;
	
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.events.Event;	
	
	public class PageStatistic extends Page
	{
		private var FMenuButton:TrackButton;
		private var FReplayButton:TrackButton;
		private var FAvatar:StatisticsAvatar;
		private var FEnemyAvatar:StatisticsAvatar;		
		
		private var FScore:TextField;
		private var FCombo:TextField;
		private var FGood:TextField;
		private var FPerfect:TextField;
		private var FBad:TextField;
		
		private var FName:TextField;
		private var FResult:TextField;
		private var FCurrentRating:TextField;
		private var FRatingClear:TextField;
		private var FRatingWithBonuses:TextField;
		private var FNewRating:TextField;
		
		private var FEnemyName:TextField;
		private var FEnemyResult:TextField;
		private var FEnemyCurrentRating:TextField;
		private var FEnemyRatingClear:TextField;
		private var FEnemyRatingWithBonuses:TextField;
		private var FEnemyNewRating:TextField;
		
		private var FEnemyScore:TextField;
		private var FEnemyCombo:TextField;
		private var FEnemyGood:TextField;
		private var FEnemyPerfect:TextField;
		private var FEnemyBad:TextField;
		
		private var FMode:int;
		
		private var FMyInfo:PointsStatisticInfo;
		private var FEnemyInfo:PointsStatisticInfo;
		private var FEnemy:PlayerInfo;
		
		public function PageStatistic():void
		{
			FMenuButton = getChildByName('MenuButton') as TrackButton;
			FReplayButton = getChildByName('ReplayButton') as TrackButton;
			FMenuButton.Text = 'Мeню';
			FReplayButton.Text = 'Повтор';
			FMenuButton.addEventListener(MouseEvent.MOUSE_DOWN, OnMenuButtonClick);
			FReplayButton.addEventListener(MouseEvent.MOUSE_DOWN, OnReplayButtonClick);
			
			FAvatar = getChildByName('Avatar') as StatisticsAvatar;
			FEnemyAvatar = getChildByName('EnemyAvatar') as StatisticsAvatar;
			
			FScore = getChildByName('ScoreText') as TextField;
			FCombo = getChildByName('ComboText') as TextField;
			FGood = getChildByName('GoodText') as TextField;
			FPerfect = getChildByName('PerfectText') as TextField;
			FBad = getChildByName('BadText') as TextField;
			
			FName = getChildByName('NameText') as TextField;
			FCurrentRating = getChildByName('CurrentRatingText') as TextField;
			FRatingClear = getChildByName('RatingClearText') as TextField;
			FRatingWithBonuses = getChildByName('RatingWithBonusesText') as TextField;
			FNewRating = getChildByName('NewRatingText') as TextField;
			
			FEnemyName = getChildByName('EnemyNameText') as TextField;
			FEnemyCurrentRating = getChildByName('EnemyCurrentRatingText') as TextField;
			FEnemyRatingClear = getChildByName('EnemyRatingClearText') as TextField;
			FEnemyRatingWithBonuses = getChildByName('EnemyRatingWithBonusesText') as TextField;
			FEnemyNewRating = getChildByName('EnemyNewRatingText') as TextField;
						
			FEnemyScore = getChildByName('EnemyScoreText') as TextField;
			FEnemyCombo = getChildByName('EnemyComboText') as TextField;
			FEnemyGood = getChildByName('EnemyGoodText') as TextField;
			FEnemyPerfect = getChildByName('EnemyPerfectText') as TextField;
			FEnemyBad = getChildByName('EnemyBadText') as TextField;
			
			FResult = getChildByName('ResultText') as TextField;
			
			FMyInfo = null;
			FEnemyInfo = null;
			FEnemy = null;
		}
				
		private function OnMenuButtonClick(e:MouseEvent):void
		{
			FGame.CurrentPage = FGame.Profile;
		}
		
		private function OnReplayButtonClick(e:MouseEvent):void
		{
			FGame.CurrentPage = FGame.Tracks;
		}
		
		public function set PhotoURL(AURL:String):void
		{
			FAvatar.Photo = AURL;
		}
		
		public function set Enemy(AEnemy:PlayerInfo):void
		{
			FEnemy = AEnemy;
			if(FEnemy!=null)
			{
				FEnemyName.text = FEnemy.Name;
				FEnemyCurrentRating.text = FEnemy.Rating;
				FEnemyAvatar.Photo = FEnemy.Photo;
			}
			else
			{
				FEnemyName.text = 'Нет противника';
				FEnemyCurrentRating.text = '0';
				FEnemyAvatar.Photo = '';
			}
		}
		
		public function set Mode(AMode:int):void
		{
			FEnemyName.text = '';
			FEnemyCurrentRating.text = '0';
			FEnemyRatingClear.text = '0';
			FEnemyRatingWithBonuses.text = '0';
			FEnemyNewRating.text = '0';
			
			FEnemyScore.text = '0';
			FEnemyCombo.text = '0';
			FEnemyGood.text = '0';
			FEnemyPerfect.text = '0';
			FEnemyBad.text = '0';
			
			FMode = AMode;
		}
		
		public function set Info(AInfo:PointsStatisticInfo):void
		{
			FMyInfo = AInfo;
			FName.text = FGame.User.Name;
			FScore.text = AInfo.Score.toString();
			FCombo.text = AInfo.Combo.toString();
			FGood.text = AInfo.Good.toString();
			FPerfect.text = AInfo.Perfect.toString();
			FBad.text = AInfo.Bad.toString();
		}
		
		public function Calculate():void
		{
			if(FGame.User.NeedMoneyBonus)
			{
				FGame.User.NeedMoneyBonus = false;
				FGame.User.Money += 15;
			}
			
			var vResultPoints:Number = new Number(0);
			var vResultWithBonus:int = 0;			
			var vEnemyResultPoints:Number = new Number(0);
			var vEnemyResultWithBonus:int = 0;			
			var vRatingsDifference:Number = new Number(0);
			var vMyPoints:Number = new Number(FMyInfo.Score);
			var vEnemyRating:int = 0;
			var vMyRating:int = int(FGame.User.Rating);
			
			if(FMode == FGame.Gameplay.TRAINING)
			{
				FResult.text = 'Тренировака';				
			}
			else
			{				
				var vEnemyPoints:Number = new Number(FEnemyInfo.Score);				
				if(FEnemy)
					vEnemyRating = int(FEnemy.Rating);				
				
				if(vMyPoints > vEnemyPoints)				
				{
					//WIN
					if(vEnemyRating > 0)
						vRatingsDifference = vEnemyRating / vMyRating;
					vResultPoints = Math.max(Math.floor((vMyPoints - vEnemyPoints) / 100) * vRatingsDifference, 1);
					if(FGame.Profile.Bonus > 0)
						vResultWithBonus = Math.floor(vResultPoints * FGame.Profile.Bonus);
					else
						vResultWithBonus = Math.floor(vResultPoints);						
										
					FGame.User.Rating = (vMyRating + vResultWithBonus).toString();
					FGame.User.Wins = (int(FGame.User.Wins) + 1).toString();			
					FResult.text = 'Победа';
				}
				else
				{
					//LOSE
					if(vEnemyRating > 0)
						vRatingsDifference = vMyRating / vEnemyRating;
					vEnemyResultPoints = Math.max(Math.floor((vEnemyPoints - vMyPoints) / 100), 1) * vRatingsDifference;					
					var vBonus:Number = new Number(FGame.Profile.ComputeBonus(FEnemy.CurrentEquipment));					
					if(vBonus > 0)
						vEnemyResultWithBonus = Math.floor(vEnemyResultPoints * vBonus);
					else
						vEnemyResultWithBonus = Math.floor(vEnemyResultPoints);
					FGame.User.Loses = (int(FGame.User.Loses) + 1).toString();
					FResult.text = 'Поражение';
				}				
				
				FEnemy = null;
				
				FGame.UpdateProfile();
				FGame.MySQL.addEventListener(FGame.MySQL.PROFILE_UPDATED, OnProfileUpdated);				
			}
			
			FCurrentRating.text = vMyRating.toString();			
			FRatingClear.text = Math.floor(vResultPoints).toString();
			FRatingWithBonuses.text = vResultWithBonus.toString();			
			FNewRating.text = FGame.User.Rating;
			
			FEnemyCurrentRating.text = vEnemyRating.toString();
			FEnemyRatingClear.text = Math.floor(vEnemyResultPoints).toString();
			FEnemyRatingWithBonuses.text = vEnemyResultWithBonus.toString();				
			FEnemyNewRating.text = (vEnemyRating + vEnemyResultWithBonus).toString();
		}
		
		private function OnProfileUpdated(e:Event):void
		{
			FGame.Top100.Update();
		}
		
		public function set EnemyInfo(AInfo:PointsStatisticInfo):void
		{
			FEnemyInfo = AInfo;	
			
			FEnemyScore.text = AInfo.Score.toString();
			FEnemyCombo.text = AInfo.Combo.toString();
			FEnemyGood.text = AInfo.Good.toString();
			FEnemyPerfect.text = AInfo.Perfect.toString();
			FEnemyBad.text = AInfo.Bad.toString();
		}
	}
}
